package com.example.mall.user.core.service.impl;

import com.example.mall.common.ServiceResultEnum;
import com.example.mall.user.api.entity.ShopUser;
import com.example.mall.user.api.service.ShopUserService;
import com.example.mall.user.core.dao.ShopUserMapper;
import com.example.mall.util.BeanUtil;
import com.example.mall.util.MD5Util;
import com.example.mall.util.PageQueryUtil;
import com.example.mall.util.PageResult;
import com.example.mall.vo.ShopUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Service
public class ShopUserServiceImpl implements ShopUserService {

    @Autowired
    private ShopUserMapper shopUserMapper;

    @Override
    public PageResult getShopUsersPage(PageQueryUtil pageUtil) {
        List<ShopUser> shopUsers = shopUserMapper.findShopUserList(pageUtil);
        int total = shopUserMapper.getTotalShopUsers(pageUtil);
        PageResult pageResult = new PageResult(shopUsers, total, (int) pageUtil.get("limit"), (int) pageUtil.get("page"));
        return pageResult;
    }

    @Override
    public String register(String loginName, String password) {
        if (shopUserMapper.selectByLoginName(loginName) != null) {
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        ShopUser registerShopUser = new ShopUser();
        registerShopUser.setLoginName(loginName);
        registerShopUser.setNickName(loginName);
        String passwordMD5 = MD5Util.MD5Encode(password, "UTF-8");
        registerShopUser.setPasswordMd5(passwordMD5);
        if (shopUserMapper.insertSelective(registerShopUser) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public ShopUserVo updateShopUserInfo(ShopUser shopUser) {
        ShopUser user = shopUserMapper.selectByPrimaryKey(shopUser.getShopUserId());
        if (user != null) {
            user.setNickName(shopUser.getNickName());
            user.setAddress(shopUser.getAddress());
            user.setShopIntro(shopUser.getShopIntro());
            if (shopUserMapper.updateByPrimaryKeySelective(user) > 0) {
                ShopUserVo shopUserVo = new ShopUserVo();
                user = shopUserMapper.selectByPrimaryKey(shopUser.getShopUserId());
                BeanUtil.copyProperties(user, shopUserVo);
                System.out.println(shopUserVo);
                return shopUserVo;
            }
        }
        return null;
    }

    @Override
    public Boolean lockShopUsers(Integer[] ids, int lockStatus) {
        if (ids.length < 1) {
            return false;
        }
        return shopUserMapper.lockShopUserBatch(ids, lockStatus) > 0;
    }

    @Override
    public ShopUser selectByLoginNameAndPassword(String loginName, String password) {
        return shopUserMapper.selectByLoginNameAndPassword(loginName, password);
    }

}
